package com.zhangxu.basemvp.base;

import android.app.Activity;
import android.app.Application;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import java.util.LinkedList;
import java.util.List;

/**
 * Time:2019/4/10
 * Author:zhangxu
 */
public class BaseApplication extends Application {
    private List<Activity> activities;
    private static BaseApplication instance;
    public BaseApplication() {
        activities = new LinkedList<>();
    }
    // 单例中获取唯一单实例；
    public static BaseApplication getInstance() {
        if (instance == null) {
            synchronized (BaseApplication.class) {
                if (instance == null) {
                    instance = new BaseApplication();
                }
            }
        }
        return instance;
    }
    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        if (activities != null && activities.size() > 0) {
            if (!activities.contains(activity)) {
                activities.add(activity);
            }
        } else {
            activities.add(activity);
        }
    }
    /**
     * 結束指定的activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activities.remove(activity);
            activity.finish();

        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.addLogAdapter(new AndroidLogAdapter());
    }
}
